// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Music, {musicName} from "../PublicScript/Music";
import GameControl_red_blue_man from "./GameControl_red_blue_man";
import {emitType} from "./GameData_red_blue_man";

const {ccclass, property} = cc._decorator;

@ccclass
export default class CoinAnim extends cc.Component {

    @property(cc.Node)
    flyCoin: cc.Node = null;
    @property(cc.Label)
    moneyLabel: cc.Label = null;
    @property(cc.Node)
    failLabel: cc.Node = null;

    coinList = [];

    point = null;
    // onLoad () {}

    start () {
        this.schedule(this.show, 0.1);
    }

    newPos = null;
    callback = null;
    setPoint(p,num, state, callback?, newPos?){
        this.point = p;
        this.callback = callback;
        this.moneyLabel.string = ":" + num;
        this.failLabel.active = !state;
        if(newPos){
            this.newPos = newPos;
        }
        cc.game.emit(emitType.animShowMoney, num);
    }

    createCoin(){
        for(let i = 0; i < 8; i++){
            let a = cc.instantiate(this.flyCoin);
            a.active = false;
            a.parent = this.node;
            this.coinList.push(a);
        }
    }


    count = 0;
    num = 0;
    animCount = 0;
    show(){
        if(this.count >= this.coinList.length-1){
            this.unschedule(this.show);
            if(this.callback){
                setTimeout(this.callback, 200)
            }
            return;
        }
        this.coinList[this.count].active = true;
        let endPos;
        if(this.newPos){
            endPos = this.newPos;
        }else{
            let pos = cc.find("Canvas").convertToWorldSpaceAR(this.point.position);
            endPos = this.node.convertToNodeSpaceAR(pos)
        }

        this.coinList[this.count].runAction(
            cc.sequence(
                cc.moveTo(0.3, endPos),
                cc.callFunc(()=>{
                    this.animCount ++;
                    this.coinList[this.count].active = false;
                    if(this.animCount >= this.coinList.length-1){
                        this.node.active = false;
                    }
                    Music.GamePlayEffect(musicName.Diamond_Sound)
                })
            )
        );
        this.count ++;
    }

    // update (dt) {}
}
